package com.ey12.db.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmMemberLevel;

/**
 * Data access object (DAO) for domain model class GmMemberLevel.
 * 
 * @see com.ey12.db.domain.GmMemberLevel
 * @author MyEclipse Persistence Tools
 */

public class GmMemberLevelDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmMemberLevelDAO.class);
	// property constants
	public static final String LEVEL_NAME = "levelName";
	public static final String WEB_GET = "webGet";
	public static final String ORDER_NUM = "orderNum";
	public static final String LEVEL_NUM = "levelNum";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmMemberLevel transientInstance) {
		log.debug("saving GmMemberLevel instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmMemberLevel persistentInstance) {
		log.debug("deleting GmMemberLevel instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmMemberLevel findById(java.lang.Integer id) {
		log.debug("getting GmMemberLevel instance with id: " + id);
		try {
			GmMemberLevel instance = (GmMemberLevel) getHibernateTemplate()
					.get("com.ey12.db.domain.GmMemberLevel", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmMemberLevel instance) {
		log.debug("finding GmMemberLevel instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmMemberLevel instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from GmMemberLevel as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findWebGetByUserLever(int userLevel){
		try {
			String queryString = "from GmMemberLevel g where g.levelNum= ?";
			return getHibernateTemplate().find(queryString, userLevel);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findWebGetBySalerLever(int userLevel){
		try {
			String queryString = "select g.webGet from GmMemberLevel g where g.levelNum= ?";
			return getHibernateTemplate().find(queryString, userLevel);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findByLevelName(Object levelName) {
		return findByProperty(LEVEL_NAME, levelName);
	}

	public List findByWebGet(Object webGet) {
		return findByProperty(WEB_GET, webGet);
	}

	public List findByOrderNum(Object orderNum) {
		return findByProperty(ORDER_NUM, orderNum);
	}

	public List findByLevelNum(Object levelNum) {
		return findByProperty(LEVEL_NUM, levelNum);
	}

	public List findAll() {
		log.debug("finding all GmMemberLevel instances");
		try {
			String queryString = "from GmMemberLevel";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findMemverLevelData() {
		try {
			String queryString = "from GmMemberLevel order by levelNum";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public GmMemberLevel merge(GmMemberLevel detachedInstance) {
		log.debug("merging GmMemberLevel instance");
		try {
			GmMemberLevel result = (GmMemberLevel) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmMemberLevel instance) {
		log.debug("attaching dirty GmMemberLevel instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmMemberLevel instance) {
		log.debug("attaching clean GmMemberLevel instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmMemberLevelDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (GmMemberLevelDAO) ctx.getBean("GmMemberLevelDAO");
	}
}